package com.oracle.controller;

import com.oracle.Service.impl.AdminService;
import com.oracle.pojo.Admin;
import com.oracle.pojo.AdminInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;

@Controller
@RequestMapping("/")
public class AdminController {

    @Autowired
    public AdminService adminService;

    @RequestMapping("/doLogin")
    public String adminLogin(Admin admin, HttpSession session, Model model){
        try{
            //验证码的正确在过滤器中完成了
            //这里主要判断账号密码是否相等
            Admin adminVo = adminService.selectAdminByUsername(admin);
            if(adminVo==null){
                model.addAttribute("adminInfo",admin.getUsername());
                model.addAttribute("loginError","账号或密码错误");
                return "/admin/login";
            }
            if (admin.getPassword()!=adminVo.getPassword()&&!admin.getPassword().equals(adminVo.getPassword())){
                model.addAttribute("adminInfo",admin.getPassword());
                model.addAttribute("loginError","账号或密码错误");
                return "/admin/login";
            }
            //如果相等则将用户存到session中
            session.setAttribute("admin",adminVo);
            return "redirect:/adminIndexView";
        }catch (Exception e){
            return "/admin/login";
        }
    }

    @RequestMapping("/adminHeadCaptcha")
    public void adminInfoHead(HttpSession session,HttpServletRequest request, HttpServletResponse response){
        FileInputStream fileInputStream = null;
        OutputStream os = null;
        try{
            Admin admin = (Admin) session.getAttribute("admin");
            AdminInfo adminInfo = adminService.selectAdminInfoByMid(admin.getMid());
            String headphoto = adminInfo.getHeadphoto();
            String url = request.getRealPath("/")+"/resource/images/"+headphoto;
            os=response.getOutputStream();
            response.setContentType("image/png");
            fileInputStream = new FileInputStream(url);
            byte[] buffer = new byte[2048];
            int l = fileInputStream.read(buffer);
            while(l != -1){
                os.write(buffer, 0, l);
                buffer = new byte[2048];
                l=fileInputStream.read(buffer);
            }
            if(os!=null){
                os.close();
            }
            if(fileInputStream!=null){
                fileInputStream.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @RequestMapping("/doShow/{mid}")
    public String adminInfoShow(@PathVariable("mid")Integer mid,Model model){

        if (mid==null||mid==0){
            return "login";
        }
        AdminInfo adminInfo = adminService.selectAdminInfoByMid(mid);

        model.addAttribute("adminInfo",adminInfo);

        return "/admin/adminInfo";
    }

    @RequestMapping("/updateAdminInfo")
    public String updateAdminInfo(MultipartFile image,AdminInfo adminInfo,HttpServletRequest request){
        if(adminInfo==null){
            request.setAttribute("addAdminInfoError","不能修改信息为空");
            return "adminInfo";
        }
        if (image.getSize()>0){
            try {
                String newFileName = UUID.randomUUID().toString().replaceAll("-","")+image.getOriginalFilename();
                image.transferTo(new File(request.getServletContext().getRealPath("/")+"resource/images/"+newFileName));
                adminInfo.setHeadphoto(newFileName);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        String result = adminService.updateAdminInfo(adminInfo);
        if (result.equals("NO")){
            request.setAttribute("addAdminInfoError","请查看修改的信息，是否填写错误");
            request.setAttribute("adminInfo",adminInfo);
            return "/admin/adminInfo";
        }
        request.getSession().setAttribute("adminInfoHead",adminInfo.getHeadphoto());
        request.setAttribute("addAdminInfoError","修改成功");
        return "redirect:/doShow/"+adminInfo.getMid();
    }

}
